package cn.agrael.struts.plugin.ejb3;

import javax.ejb.EJBException;

import org.apache.struts2.impl.StrutsActionProxyFactory;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;

/**
 * 基于 EJB3.0 功能的 ActionProxyFactory 实现。
 * 
 * @author <a href="mailto:agraellee@gmail.com">Agrael·Lee</a>
 * 
 */
public class StrutsEJBActionProxyFactory extends StrutsActionProxyFactory {

	@Override
	public ActionProxy createActionProxy(ActionInvocation inv, String namespace, String actionName, String methodName,
			boolean executeResult, boolean cleanupContext) {
		ActionProxy proxy = super.createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
		Object action = inv.getAction();
		try {
			// 执行 EJB 的注解
			BeanEJBAnnotation beanEJBAnnotation = StrutsEJBUtils.executeEJBInjectAnnotation(action);
			if (!beanEJBAnnotation.isEmpty()) {
				// 如果注解信息不为空，模拟生命周期方法
				StrutsEJBUtils.simulation(action, beanEJBAnnotation);
				// 则创建代理执行
				proxy = StrutsEJBUtils.createActionAnnotationProxy(proxy, beanEJBAnnotation);
			}
		} catch (Exception e) {
			throw new EJBException(e);
		}
		return proxy;
	}

}
